home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / snz128s / src / mail.c < prev    next >
C/C++ Source or Header  |  1994-05-28  |  32KB  |  1,233 lines

  1. /*
  2.     SNEWS 2.0
  3.  
  4.     Mail - routines to mail from news
  5.  
  6.  
  7.     Copyright (C) 1991  John McCombs, Christchurch, NEW ZEALAND
  8.                         john@ahuriri.gen.nz
  9.                         PO Box 2708, Christchurch, NEW ZEALAND
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License, version 1, as
  13.     published by the Free Software Foundation.
  14.  
  15.     This program is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.     GNU General Public License for more details.
  19.  
  20.     See the file COPYING, which contains a copy of the GNU General
  21.     Public License.
  22.  
  23.     Atari version ported by Graham Judd - gjudd@siward.demon.co.uk
  24.  */
  25.  
  26. /*---------------------------- Source Control ------------------------------*/
  27.  
  28. /*
  29.  * $Id: MAIL.C,v 1.2 1994/02/05 18:48:42 gbj Exp user $
  30.  */
  31.  
  32. /****************************************************************************
  33. *   28 Jun 93   1.01   MSM  Initial extraction from article.c               *
  34. *   11 Aug 93   1.02   MSM  Expert mode added                               *
  35. *   22 Aug 93   1.03   MSM  Limited alias support added                     *
  36. *   26 Aug 93   1.04   MSM  Bug in mailer header detection fixed            *
  37. *    3 Oct 93   1.05   MSM  Extra abort options                             *
  38. *   24 Oct 93    1.06   MSM  Bug in alias handling fixed                     *
  39. *    4 Dec 93   1.07   MSM  Permit blank organisation                       *
  40. *   31 Jan 94   AT1    GBJ  News is now posted by nntp in post.c            *
  41. *    2 Apr 94   1.08   MSM  Support optional seperator and footer quote     *
  42. ****************************************************************************/
  43.  
  44.  
  45. #include "defs.h"
  46. #include "snews.h"
  47. #include "screen.h"
  48. #include "locking.h"
  49.  
  50. #ifdef ATARI
  51. #    include "fileops.h"
  52. #endif
  53.  
  54. #include <io.h>
  55. #include <ctype.h>
  56.  
  57. long seqno;
  58.  
  59. extern char marker[];
  60.  
  61. /*-------------------------- reply to article ---------------------------*/
  62. void            reply_to_article(TEXT * tx, char *subject)
  63. {
  64.  
  65.     /*
  66.      * Mail reply to article
  67.      */
  68.  
  69. #ifdef INCLUDE_SIG
  70.     FILE           *sig;
  71.     char            sig_fn[80];
  72.  
  73. #endif
  74.  
  75.     FILE           *tmp, *new_tmp;
  76.     LINES          *ln;
  77.     WRK_FILE       *wrk;
  78.     int             ch, lct;
  79.     char            fn[80];
  80.     char            buf[256], buf2[256];
  81.     char            author[80], msg_id[80], r_name[128];
  82.     int             rc, header;
  83.  
  84.     header = FALSE;
  85.     subject = subject;                      /* silence warning */
  86.     strcpy(fn, "reply.tmp");
  87.     wrk = NULL;
  88.  
  89.     if ((tmp = fopen(fn, "wt")) != NULL) {
  90.  
  91.         lct = 1;
  92.         get_his_stuff(tx, author, msg_id, r_name);
  93.  
  94.         if (my_stuff.expert == TRUE) {
  95.             if (make_header(tmp, subject, author, NULL, NULL, &lct) != 0) {
  96.                 fclose(tmp);
  97.                 unlink(fn);
  98.                 return;
  99.             }
  100.         }
  101.         else {
  102.             command("");
  103.             sprintf(buf, "Reply to %s (y/n) ", author);
  104.             lmessage(buf);
  105.             do
  106.                 {
  107.                 ch = getch();
  108.                 ch = tolower(ch);
  109.             }
  110.             while ((ch != 'y') && (ch != 'n'));
  111.             if (ch == 'n') {
  112.                 lmessage("Reply to whom ? ");
  113.                 gets(author);
  114.             }
  115.         }
  116.  
  117.         /* add the quoted message */
  118.         message("Quote article (y/n/a)? ");
  119.         do
  120.             {
  121.             ch = getch();
  122.             ch = tolower(ch);
  123.         }
  124.         while ((ch != 'y') && (ch != 'n') && (ch != 'a'));
  125.         textbackground(msgb);
  126.         textcolor(msgf);
  127.         putch(ch);
  128.         putch('\n');
  129.         putch('\r');
  130.         textbackground(textb);
  131.         textcolor(textf);
  132.  
  133.         if (ch == 'a') {
  134.             fclose(tmp);
  135.             unlink(fn);
  136.             message("*** Mail message aborted - Press any key ***");
  137.             getch();
  138.             return;
  139.         }
  140.         if (ch == 'y') {
  141.             fprintf(tmp, "In article %s you write:\n\n", msg_id);
  142.             if (my_stuff.header_quote)
  143.                 ln = tx->top;
  144.             else
  145.                 ln = tx->start;
  146.             if (ln->data == '\0')
  147.                 ln = ln->next;
  148.             while (ln != NULL) {
  149.                 if ((strncmp(ln->data, "-- ", 3) == 0) && my_stuff.footer_quote)
  150.                     break;
  151.                 fprintf(tmp, "%s%s", my_stuff.quotemark, ln->data);
  152.                 ln = ln->next;
  153.             }
  154.         }
  155. #ifdef INCLUDE_SIG
  156.  
  157.         /* append the signature if there is one */
  158.         strcpy(sig_fn, my_stuff.home);
  159.         strcat(sig_fn, my_stuff.signature);
  160.         if ((sig = fopen(sig_fn, "rt")) != NULL) {
  161.             if (my_stuff.footer_quote)
  162.                 fprintf(tmp, "\n-- \n");
  163.             else
  164.                 fprintf(tmp, "\n");
  165.             while (fgets(buf, 79, sig) != NULL)
  166.                 fputs(buf, tmp);
  167.  
  168.             (void) fclose(sig);
  169.         }
  170. #endif
  171.         fclose(tmp);
  172.         ch = 'e';
  173.         while ((ch != 's') && (ch != 'a')) {
  174.             if (ch == 'e') {
  175.                 sprintf(buf, my_stuff.editor, fn);
  176.                 strcpy(buf2, buf);
  177.                 if (my_stuff.edit_line != NULL) {
  178.                     strcat(buf2, " ");
  179.                     if (header == TRUE)
  180.                         sprintf(buf, my_stuff.edit_line, lct+1);
  181.                     else
  182.                         sprintf(buf, my_stuff.edit_line, lct);
  183.                     strcat(buf2, buf);
  184.                 }
  185.                 rc = system(buf2);
  186.                 if (rc != 0) {
  187.                     unlink(fn);
  188.                     return;
  189.                 }
  190.             }
  191.  
  192.             if ((my_stuff.expert != TRUE) && (header == FALSE)) {
  193.                 if ((tmp = fopen(fn, "r")) == NULL) {
  194.                     unlink(fn);
  195.                     return;
  196.                 }
  197.                 if ((new_tmp = fopen("new.tmp", "w+")) == NULL) {
  198.                     unlink(fn);
  199.                     return;
  200.                 }
  201.                 if (make_header(new_tmp, subject, author, NULL, NULL, &lct) != 0)
  202.                     return;
  203.                 while (fgets(buf, sizeof(buf), tmp) != NULL) {
  204.                     fputs(buf, new_tmp);
  205.                 }
  206.                 fclose(tmp);
  207.                 fclose(new_tmp);
  208.                 unlink(fn);
  209.                 rename("new.tmp", fn);
  210.                 header = TRUE;
  211.             }
  212.  
  213.             gotoxy(1, scr_rows - 2);
  214.             textbackground(msgb);
  215.             textcolor(msgf);
  216.             clreol();
  217.             printf("Mail reply ?");
  218.             textbackground(textb);
  219.             textcolor(textf);
  220.             sprintf(buf, "(S)end, (A)bort or (E)dit again ? (s/a/e): ");
  221.             message(buf);
  222.             do {
  223.                 ch = getch();
  224.                 ch = tolower(ch);
  225.             }
  226.             while ((ch != 's') && (ch != 'a') && (ch != 'e'));
  227.             gotoxy(1, scr_rows);
  228.         }
  229.         gotoxy(1, scr_rows - 2);
  230.         textbackground(msgb);
  231.         textcolor(msgf);
  232.         clreol();
  233.         if (ch == 's') {
  234.             wrk = parse(fn, TRUE);
  235.             if (wrk == NULL) {
  236.                 unlink(fn);
  237.                 message("*** Mail message aborted - Press any key ***");
  238.                 getch();
  239.                 return;
  240.             }
  241.             pass_to_smtp(fn, wrk, TRUE);
  242.             message("*** Message sent - Press any key ***");
  243.             getch();
  244.         }
  245.         if (ch == 'a') {
  246.             message("*** Mail message aborted - Press any key ***");
  247.             getch();
  248.         }
  249.         unlink(fn);
  250.  
  251.     }
  252.     else {
  253.         message("*** Cannot open temp file - press any key to continue ***");
  254.         getch();
  255.     }
  256.  
  257. }
  258.  
  259. /* ---------- (R)eply allowing specified address or alias ---------------- */
  260.  
  261. void            ReplyAddress(TEXT * tx, char *subject)
  262. {
  263. #ifdef INCLUDE_SIG
  264.     FILE           *sig;
  265.     char            sig_fn[80];
  266.  
  267. #endif
  268.  
  269.     FILE           *tmp, *new_tmp;
  270.     WRK_FILE       *wrk;
  271.  
  272.     FILE           *alias;
  273.     char           *s;
  274.  
  275.     LINES          *ln;
  276.     int             ch, lct;
  277.     char            fn[80];
  278.     char            buf[256], buf2[256], buf3[24];
  279.     char            who_to[1024], author[80], msg_id[80], r_name[128];
  280.     int             rc, header;
  281.  
  282.     header = FALSE;
  283.     strcpy(fn, "reply.tmp");
  284.     wrk = NULL;
  285.  
  286.     if ((tmp = fopen(fn, "wt")) != NULL) {
  287.  
  288.         lct = 1;
  289.  
  290.         sprintf(buf, "Send reply to whom: ");
  291.         lmessage(buf);
  292.         gets(who_to);
  293.         if (strlen(who_to) == 0)
  294.             return;
  295.  
  296.         /* look for an alias */
  297.  
  298.         if (strlen(my_stuff.alias_file) != 0) {
  299.             if ((alias = fopen(my_stuff.alias_file, "rt")) != NULL) {
  300.                 while (fgets(buf, 255, alias) != NULL) {
  301.                     if (buf[0] == ';' || buf[0] == '#' || buf[0] == '\n')
  302.                         continue;
  303.                     s = strtok(buf, " \t,");
  304.                     if (s == NULL)
  305.                         continue;
  306.                     if (stricmp(who_to, s) == 0) {
  307.                         who_to[0] = '\0';
  308.                         do
  309.                             {
  310.                             while ((s = strtok(NULL, " \t,")) != NULL) {
  311.                                 strcat(who_to, s);
  312.                                 if (who_to[strlen(who_to)-1] != '\n')
  313.                                     strcat(who_to, ",");
  314.                                 else
  315.                                     who_to[strlen(who_to)-1] = '\0';
  316.                             }
  317.                             if (fgets(buf, 255, alias) == NULL)
  318.                                 break;
  319.                             if (buf[0] != ' ')
  320.                                 break;
  321.                             s = strtok(buf, " \t");
  322.                             if (s != NULL) {
  323.                                 strcat(who_to, ",");
  324.                                 strcat(who_to, s);
  325.                                 if (who_to[strlen(who_to)-1] != '\n')
  326.                                     strcat(who_to, ",");
  327.                                 else
  328.                                     who_to[strlen(who_to)-1] = '\0';
  329.                             }
  330.                         }
  331.                         while (s != NULL);
  332.                     }
  333.                     }
  334.             fclose(alias);
  335.             }
  336.         }
  337.  
  338.         if (strchr(who_to, '@') == NULL) {
  339.             sprintf(buf, "@%s.%s", my_stuff.my_site, my_stuff.my_domain);
  340.             strcat(who_to, buf);
  341.         }
  342.  
  343.         if (my_stuff.expert == TRUE)
  344.             if (make_header(tmp, subject, who_to, NULL, NULL, &lct) != 0) {
  345.                 fclose(tmp);
  346.                 unlink(fn);
  347.                 return;
  348.             }
  349.  
  350.         get_his_stuff(tx, author, msg_id, r_name);
  351.  
  352.         /* add the quoted message */
  353.         message("Quote article (y/n/a)? ");
  354.         do
  355.             {
  356.             ch = getch();
  357.             ch = tolower(ch);
  358.         }
  359.         while ((ch != 'y') && (ch != 'n') && (ch != 'a'));
  360.         textbackground(msgb);
  361.         textcolor(msgf);
  362.         putch(ch);
  363.         putch('\n');
  364.         putch('\r');
  365.         textbackground(textb);
  366.         textcolor(textf);
  367.  
  368.         if (ch == 'a') {
  369.             fclose(tmp);
  370.             unlink(fn);
  371.             message("*** Mail message aborted - Press any key ***");
  372.             getch();
  373.             return;
  374.         }
  375.         if (ch == 'y') {
  376.             strcpy(buf2, " ");
  377.             strcpy(buf3, " ");
  378.             if ((strlen(msg_id) + strlen(author) + strlen(r_name)) > 60) {
  379.                 strcpy(buf2, "\n           ");
  380.                 if ((strlen(author) + strlen(r_name)) > 60)
  381.                     strcpy(buf3, "\n           ");
  382.  
  383.             }
  384.             fprintf(tmp, "In article %s%s%s%s%s writes:\n\n", msg_id, buf2, author, buf3, r_name);
  385.             if (my_stuff.header_quote)
  386.                 ln = tx->top;
  387.             else
  388.                 ln = tx->start;
  389.             if (ln->data == '\0')
  390.                 ln = ln->next;
  391.             while (ln != NULL) {
  392.                 if ((strncmp(ln->data, "-- ", 3) == 0) && my_stuff.footer_quote)
  393.                     break;
  394.                 fprintf(tmp, " > %s", ln->data);
  395.                 ln = ln->next;
  396.             }
  397.         }
  398. #ifdef INCLUDE_SIG
  399.         /* append the signature if there is one */
  400.         strcpy(sig_fn, my_stuff.home);
  401.         strcat(sig_fn, my_stuff.signature);
  402.         if ((sig = fopen(sig_fn, "rt")) != NULL) {
  403.             if (my_stuff.footer_quote)
  404.                 fprintf(tmp, "\n-- \n");
  405.             else
  406.                 fprintf(tmp, "\n");
  407.             while (fgets(buf, 79, sig) != NULL)
  408.                 fputs(buf, tmp);
  409.  
  410.             fclose(sig);
  411.         }
  412. #endif
  413.         fclose(tmp);
  414.  
  415.         ch = 'e';
  416.         while ((ch != 's') && (ch != 'a')) {
  417.             if (ch == 'e') {
  418.                 sprintf(buf, my_stuff.editor, fn);
  419.                 strcpy(buf2, buf);
  420.                 if (my_stuff.edit_line != NULL) {
  421.                     strcat(buf2, " ");
  422.                     if (header ==  TRUE)
  423.                         sprintf(buf, my_stuff.edit_line, lct+1);
  424.                     else
  425.                         sprintf(buf, my_stuff.edit_line, lct);
  426.                     strcat(buf2, buf);
  427.                 }
  428.                 rc = system(buf2);
  429.                 if (rc != 0) {
  430.                     unlink(fn);
  431.                     return;
  432.                 }
  433.             }
  434.  
  435.             if ((my_stuff.expert != TRUE) && (header == FALSE)) {
  436.                 if ((tmp = fopen(fn, "r")) == NULL) {
  437.                     unlink(fn);
  438.                     return;
  439.                 }
  440.                 if ((new_tmp = fopen("new.tmp", "w+")) == NULL) {
  441.                     unlink(fn);
  442.                     return;
  443.                 }
  444.                 if (make_header(new_tmp, subject, who_to, NULL, NULL, &lct) != 0)
  445.                     return;
  446.                 while (fgets(buf, sizeof(buf), tmp) != NULL) {
  447.                     fputs(buf, new_tmp);
  448.                 }
  449.                 fclose(tmp);
  450.                 fclose(new_tmp);
  451.                 unlink(fn);
  452.                 rename("new.tmp", fn);
  453.                 header = TRUE;
  454.             }
  455.  
  456.             gotoxy(1, scr_rows - 2);
  457.             textbackground(msgb);
  458.             textcolor(msgf);
  459.             clreol();
  460.             printf("Mail article ?");
  461.             textbackground(textb);
  462.             textcolor(textf);
  463.             sprintf(buf, "(S)end, (A)bort, or (E)dit again? (S/A/E):");
  464.             message(buf);
  465.             do {
  466.                 ch = getch();
  467.                 ch = tolower(ch);
  468.             }
  469.             while ((ch != 's') && (ch != 'a') && (ch != 'e'));
  470.             gotoxy(1, scr_rows);
  471.         }
  472.         gotoxy(1, scr_rows - 2);
  473.         textbackground(msgb);
  474.         textcolor(msgf);
  475.         clreol();
  476.         if (ch == 's') {
  477.             wrk = parse(fn, TRUE);
  478.             if (wrk == NULL) {
  479.                 message("*** Mail message aborted - Press any key ***");
  480.                 getch();
  481.                 unlink(fn);
  482.                 return;
  483.             }
  484.             pass_to_smtp(fn, wrk, TRUE);
  485.             message("*** Mail sent - press any key ***");
  486.             getch();
  487.         }
  488.         if (ch == 'a') {
  489.             message("*** Mail message aborted - Press any key ***");
  490.             getch();
  491.         }
  492.  
  493.         unlink(fn);
  494.  
  495.     }
  496.     else {
  497.         message("*** Cannot open temp file - press any key to continue ***");
  498.         getch();
  499.     }
  500.  
  501. }
  502.  
  503.  
  504.  
  505.  
  506. /*-------------------------- mail this article ---------------------------*/
  507. void            mail_to_someone(TEXT * tx)
  508. {
  509.  
  510.     /*
  511.      * Mail this article to someone
  512.      */
  513.  
  514.  
  515.     FILE           *tmp, *new_tmp;
  516.     LINES          *ln;
  517.     WRK_FILE       *wrk;
  518.     int             ch, rc, lct, header;
  519.     char            fn[80];
  520.     char            buf[256], buf2[256], who[1024];
  521.     char            subject[80], author[80], msg_id[80], r_name[128];
  522.  
  523.     FILE           *alias;
  524.     char           *s;
  525.  
  526. #ifdef INCLUDE_SIG
  527.     FILE           *sig;
  528.     char            sig_fn[80];
  529. #endif
  530.  
  531.     header = FALSE;
  532.     strcpy(fn, "reply.tmp");
  533.     wrk = NULL;
  534.  
  535.     if ((tmp = fopen(fn, "wt")) != NULL) {
  536.  
  537.         lct = 1;
  538.  
  539.         if (tx != NULL) {
  540.             get_his_stuff(tx, author, msg_id, r_name);
  541.  
  542.             command("");
  543.             lmessage("To whom do you want to mail this article ? ");
  544.             gets(who);
  545.             if (strlen(who) == 0) {
  546.                  fclose(tmp);
  547.                 unlink("reply.tmp");
  548.                 return;
  549.             }
  550.  
  551.             /* look for an alias */
  552.  
  553.             if (strlen(my_stuff.alias_file) != 0) {
  554.                 if ((alias = fopen(my_stuff.alias_file, "rt")) != NULL) {
  555.                     while (fgets(buf, 255, alias) != NULL) {
  556.                         if (buf[0] == ';' || buf[0] == '#' || buf[0] == '\n')
  557.                             continue;
  558.                         s = strtok(buf, " \t,");
  559.                         if (s == NULL)
  560.                             continue;
  561.                         if (stricmp(who, s) == 0) {
  562.                             who[0] = '\0';
  563.                             do
  564.                                 {
  565.                                 while ((s = strtok(NULL, " \t,")) != NULL) {
  566.                                     strcat(who, s);
  567.                                     if (who[strlen(who)-1] != '\n')
  568.                                         strcat(who, ",");
  569.                                     else
  570.                                         who[strlen(who)-1] = '\0';
  571.                                 }
  572.                                 if (fgets(buf, 255, alias) == NULL)
  573.                                     break;
  574.                                 if (buf[0] != ' ')
  575.                                     break;
  576.                                 s = strtok(buf, " \t");
  577.                                 if (s != NULL) {
  578.                                     strcat(who, ",");
  579.                                     strcat(who, s);
  580.                                     if (who[strlen(who)-1] != '\n')
  581.                                         strcat(who, ",");
  582.                                     else
  583.                                         who[strlen(who)-1] = '\0';
  584.                                 }
  585.                             }
  586.                             while (s != NULL);
  587.                         }
  588.  
  589.                     }
  590.                 fclose(alias);
  591.                 }
  592.             }
  593.  
  594.             if (strchr(who, '@') == NULL) {
  595.                 sprintf(buf, "@%s.%s", my_stuff.my_site, my_stuff.my_domain);
  596.                 strcat(who, buf);
  597.             }
  598.  
  599.             if (my_stuff.expert == TRUE)
  600.                 if (make_header(tmp, "Forwarded article.", who, NULL, NULL, &lct) != 0) {
  601.                      fclose(tmp);
  602.                     unlink("reply.tmp");
  603.                     return;
  604.                 }
  605.  
  606.             /* add the quoted message */
  607.             fprintf(tmp, "This article was forwarded to you by %s@%s.%s (%s):\n\n",
  608.                     my_stuff.user, my_stuff.my_site, my_stuff.my_domain, my_stuff.my_name);
  609.             fprintf(tmp, "--------------------------------- cut here -----------------------------\n\n");
  610.             ln = tx->top;
  611.             while (ln != NULL) {
  612.                 fprintf(tmp, "%s", ln->data);
  613.                 ln = ln->next;
  614.             }
  615.  
  616.             fprintf(tmp, "--------------------------------- cut here -----------------------------\n\n");
  617.  
  618.         }
  619.         else {
  620.             command("");
  621.             lmessage("Mail to whom ? ");
  622.             gets(who);
  623.             if (strlen(who) == 0) {
  624.                  fclose(tmp);
  625.                 unlink("reply.tmp");
  626.                 return;
  627.             }
  628.             lmessage("Subject ? ");
  629.             gets(subject);
  630.  
  631.             /* look for an alias */
  632.  
  633.             if (strlen(my_stuff.alias_file) != 0) {
  634.                 if ((alias = fopen(my_stuff.alias_file, "rt")) != NULL) {
  635.                     while (fgets(buf, 255, alias) != NULL) {
  636.                         if (buf[0] == ';' || buf[0] == '#' || buf[0] == '\n')
  637.                             continue;
  638.                         s = strtok(buf, " \t,");
  639.                         if (s == NULL)
  640.                             continue;
  641.                         if (stricmp(who, s) == 0) {
  642.                             who[0] = '\0';
  643.                             do
  644.                                 {
  645.                                 while ((s = strtok(NULL, " \t,")) != NULL) {
  646.                                     strcat(who, s);
  647.                                     if (who[strlen(who)-1] != '\n')
  648.                                         strcat(who, ",");
  649.                                     else
  650.                                         who[strlen(who)-1] = '\0';
  651.                                 }
  652.                                 if (fgets(buf, 255, alias) == NULL)
  653.                                     break;
  654.                                 if (buf[0] != ' ')
  655.                                     break;
  656.                                 s = strtok(buf, " \t");
  657.                                 if (s != NULL) {
  658.                                     strcat(who, ",");
  659.                                     strcat(who, s);
  660.                                     if (who[strlen(who)-1] != '\n')
  661.                                         strcat(who, ",");
  662.                                     else
  663.                                         who[strlen(who)-1] = '\0';
  664.                                 }
  665.                             }
  666.                             while (s != NULL);
  667.                         }
  668.  
  669.                     }
  670.                 fclose(alias);
  671.                 }
  672.             }
  673.  
  674.             if (strchr(who, '@') == NULL) {
  675.                 sprintf(buf, "@%s.%s", my_stuff.my_site, my_stuff.my_domain);
  676.                 strcat(who, buf);
  677.             }
  678.  
  679.             if (my_stuff.expert == TRUE )
  680.                 if (make_header(tmp, subject, who, NULL, NULL, &lct) != 0) {
  681.                      fclose(tmp);
  682.                     unlink("reply.tmp");
  683.                     return;
  684.                 }
  685.  
  686. #ifdef INCLUDE_SIG
  687.             /* append the signature if there is one */
  688.             strcpy(sig_fn, my_stuff.home);
  689.             strcat(sig_fn, my_stuff.signature);
  690.             if ((sig = fopen(sig_fn, "rt")) != NULL) {
  691.                 if (my_stuff.footer_quote)
  692.                     fprintf(tmp, "\n-- \n");
  693.                 else
  694.                     fprintf(tmp, "\n");
  695.                 while (fgets(buf, 79, sig) != NULL)
  696.                     fputs(buf, tmp);
  697.  
  698.                 fclose(sig);
  699.             }
  700. #endif
  701.  
  702.         }
  703.         fclose(tmp);
  704.  
  705.         ch = 'e';
  706.         while ((ch != 's') && (ch != 'a')) {
  707.             if (ch == 'e') {
  708.                 sprintf(buf, my_stuff.editor, fn);
  709.                 strcpy(buf2, buf);
  710.                 if (my_stuff.edit_line != NULL) {
  711.                     strcat(buf2, " ");
  712.                     sprintf(buf, my_stuff.edit_line, lct);
  713.                     strcat(buf2, buf);
  714.                 }
  715.                 rc = system(buf2);
  716.                 if (rc != 0) {
  717.                     unlink(fn);
  718.                     return;
  719.                 }
  720.             }
  721.  
  722.             if ((my_stuff.expert != TRUE) && (header == FALSE)) {
  723.                 if ((tmp = fopen(fn, "r")) == NULL) {
  724.                     unlink(fn);
  725.                     return;
  726.                 }
  727.                 if ((new_tmp = fopen("new.tmp", "w+")) == NULL) {
  728.                     unlink(fn);
  729.                     return;
  730.                 }
  731.                 if (tx != NULL) {
  732.                     if (make_header(new_tmp, "Forwarded Article", who, NULL, NULL, &lct) != 0)
  733.                         return;
  734.                 }
  735.                 else {
  736.                     if (make_header(new_tmp, subject, who, NULL, NULL, &lct) != 0) 
  737.                         return;
  738.                 }
  739.                 while (fgets(buf, sizeof(buf), tmp) != NULL) {
  740.                     fputs(buf, new_tmp);
  741.                 }
  742.                 fclose(tmp);
  743.                 fclose(new_tmp);
  744.                 unlink(fn);
  745.                 rename("new.tmp", fn);
  746.                 header = TRUE;
  747.             }
  748.  
  749.             gotoxy(1, scr_rows - 2);
  750.             textbackground(msgb);
  751.             textcolor(msgf);
  752.             clreol();
  753.             cprintf("Send message ?");
  754.             textbackground(textb);
  755.             textcolor(textf);
  756.             gotoxy(1, scr_rows - 1);
  757.             sprintf(buf, "(S)end, (A)bort or (E)dit again ? (s/a/e):");
  758.             message(buf);
  759.             do
  760.                 {
  761.                 ch = getch();
  762.                 ch = tolower(ch);
  763.             }
  764.             while ((ch != 'a') && (ch != 'e') && (ch != 's'));
  765.         }
  766.         gotoxy(1, scr_rows - 2);
  767.         textbackground(msgb);
  768.         textcolor(msgf);
  769.         clreol();
  770.         if (ch == 's') {
  771.             wrk = parse(fn, TRUE);
  772.             if (wrk == NULL) {
  773.                 unlink(fn);
  774.                 message("*** Mail message aborted - Press any key ***");
  775.                 getch();
  776.                 return;
  777.             }
  778.             pass_to_smtp(fn, wrk, TRUE);
  779.             message("*** Mail sent - Press any key ***");
  780.             getch();
  781.         }
  782.  
  783.         if (ch == 'a') {
  784.             message("*** Mail message aborted - Press any key ***");
  785.             getch();
  786.         }
  787.         unlink(fn);
  788.  
  789.     }
  790.     else {
  791.         message("*** Cannot open temp file - press any key to continue ***");
  792.         getch();
  793.     }
  794. }
  795.  
  796. /*--------------------- Add a header to the file ---------------------------*/
  797. int                 make_header(FILE *tmp, char *subject, char *author, char *cc,
  798.                 char *bc, int *lct)
  799. {
  800.     char            buf[256];
  801.     time_t          t;
  802.     struct tm      *gmt;
  803.     FILE           *seq_fp;
  804.  
  805.     
  806.     strcpy(buf, my_stuff.spooldir);
  807.     strcat(buf, "sequence.seq");
  808.     if (mlock(my_stuff.spooldir, "sequence", "Snews") != 0) {
  809.         message("*** Couldn't lock sequence file - press any key ***");
  810.         getch();
  811.         fclose(tmp);
  812.         return -1;
  813.     }
  814.     if ((seq_fp = fopen(buf, "r+")) == NULL) {
  815.         message("*** Couldn't open mail sequence file - press any key ***");
  816.         getch();
  817.         fclose(tmp);
  818.         return -1;
  819.     }
  820.     /* Get the next sequence number */
  821.     rewind(seq_fp);
  822.     fscanf(seq_fp, "%ld", &seqno);
  823.     seqno++;
  824.     rewind(seq_fp);
  825.     fprintf(seq_fp, "%ld\n", seqno);
  826.     fclose(seq_fp);
  827.     rmlock(my_stuff.spooldir, "sequence", "Snews");
  828.     time(&t);
  829.     gmt = gmtime(&t);
  830.     fprintf(tmp, "Date: %s, %02d %s %02d %02d:%02d:%02d GMT\n",
  831.         dow[gmt->tm_wday],
  832.         gmt->tm_mday, mth[gmt->tm_mon], (gmt->tm_year % 100),
  833.         gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
  834.     (*lct)++;
  835.  
  836.     fprintf(tmp, "Message-ID: <%ld@%s.%s>\n", seqno, my_stuff.my_site,
  837.         my_stuff.my_domain);
  838.     (*lct)++;
  839.     fprintf(tmp, "From: %s@%s.%s (%s)\n", my_stuff.user,
  840.         my_stuff.my_site, my_stuff.my_domain,
  841.         my_stuff.my_name);
  842.     (*lct)++;
  843. #ifdef ATARI
  844.     if ((my_stuff.my_organisation != NULL) && (*my_stuff.my_organisation != '\0'))
  845. #else
  846.     if ((*my_stuff.my_organisation != '\0') && (my_stuff.my_organisation != NULL))
  847. #endif
  848.         fprintf(tmp, "Organization: %s\n", my_stuff.my_organisation);
  849.     else
  850.         fprintf(tmp, "Organization: \n");
  851.     (*lct)++;
  852. #ifdef ATARI
  853.     if ((my_stuff.replyuser != NULL) && (*my_stuff.replyuser != '\0'))
  854. #else
  855.     if (*my_stuff.replyuser == '\0')
  856. #endif
  857.         fprintf(tmp, "Reply-To: %s@%s.%s\n", my_stuff.user,
  858.             my_stuff.my_site, my_stuff.my_domain);
  859.     else
  860.         fprintf(tmp, "Reply-To: %s\n", my_stuff.replyuser);
  861.     (*lct)++;
  862.     fprintf(tmp, "To: %s\n", author);
  863.     (*lct)++;
  864.     if (cc != NULL)
  865.         fprintf(tmp, "Cc: %s\n", cc);
  866.     else
  867.         fprintf(tmp, "Cc: \n");
  868.     (*lct)++;
  869.     if (bc != NULL)
  870.         fprintf(tmp, "Bcc: %s\n", bc);
  871.     else
  872.         fprintf(tmp, "Bcc: \n");
  873.     (*lct)++;
  874.     fprintf(tmp, "Subject: %s\n", subject);
  875.     (*lct)++;
  876.     fprintf(tmp, "X-Mailer: %s\n", VERSION);
  877.     (*lct)++;
  878.     fprintf(tmp, "%s", marker);
  879.     (*lct)++;
  880.     return 0;
  881. }
  882.  
  883. /*--------------------- Parse a message for WRK files ----------------------*/
  884.  
  885. WRK_FILE          *parse(char *fname, int ismail)
  886. {
  887.     WRK_FILE           *wrk, *wrk_start;
  888.     FILE               *fp, *seq_fp;
  889.     char                buff[512], buf2[80], to[80], buf3[80];
  890.     int                 isto, isfrom, isnewsgps, isid, need_edit;
  891.     int                 isnottoken, in_header, ch, i;
  892.     char               *p, *q;
  893.     long                where;
  894.  
  895.     isto = isfrom = isnewsgps = isid =    need_edit = FALSE;
  896.     wrk = wrk_start = NULL;
  897.  
  898.     if ((fp = fopen(fname,"rt")) != NULL) {
  899.         do
  900.             {
  901.             need_edit = FALSE;
  902.             in_header = TRUE;
  903.             isnottoken = FALSE;
  904.             while (fgets(buff, 511, fp) != NULL) {
  905.                 if (strncmp(buff, marker, 80) == 0) {
  906.                     in_header = FALSE;
  907.                     break;
  908.                 }
  909.                 if (in_header == TRUE) {
  910.                     if ((strnicmp(buff, "TO:", 3) == 0) && (strlen(buff) > 6))
  911.                         isto = TRUE;
  912.                     if ((strnicmp(buff, "FROM:", 5) == 0) && (strlen(buff) > 8))
  913.                         isfrom = TRUE;
  914.                     if ((strnicmp(buff, "MESSAGE-ID:", 11) == 0) && (strlen(buff) > 14))
  915.                         isid  = TRUE;
  916.                     if ((strnicmp(buff, "NEWSGROUPS:", 11) == 0) && (strlen(buff) > 14))
  917.                         isnewsgps = TRUE;
  918.                     if ((strchr(buff, ':') == NULL) && (buff[0] != ' '))
  919.                         isnottoken = TRUE;
  920.                 }
  921.             }
  922.  
  923.             if (in_header == TRUE) {
  924.                 message("*** End of header not found - press any key ***");
  925.                 getch();
  926.                 need_edit = TRUE;
  927.             }
  928.             if (isnottoken == TRUE) {
  929.                 message("*** Invalid header line found - press any key ***");
  930.                 getch();
  931.                 need_edit = TRUE;
  932.             }
  933.             if (isto == FALSE) {
  934.                 message("*** No 'To:' line in header - press any key ***");
  935.                 getch();
  936.                 need_edit = TRUE;
  937.             }
  938.             if (isfrom == FALSE) {
  939.                 message("*** No 'From:' line in header - press any key ***");
  940.                 getch();
  941.                 need_edit = TRUE;
  942.             }
  943.             if (isid == FALSE) {
  944.                 message("*** No 'Message-ID:' line in header - press any key ***");
  945.                 getch();
  946.                 need_edit = TRUE;
  947.             }
  948.             if ((isnewsgps == FALSE) && (!ismail)) {
  949.                 message("*** No 'Newsgroups:' line in header - press any key ***");
  950.                 getch();
  951.                 need_edit = TRUE;
  952.             }
  953.             if (need_edit == TRUE) {
  954.                 fclose(fp);
  955.                 message("(E)dit or (A)bort (e/a) ? ");
  956.                 do
  957.                     {
  958.                     ch = getch();
  959.                     ch = tolower(ch);
  960.                 }
  961.                 while((ch != 'e') && (ch != 'a'));
  962.                 if (ch == 'a')
  963.                     return NULL;
  964.                 sprintf(buff, my_stuff.editor, fname);
  965.                 system(buff);
  966.                 if ((fp = fopen(fname, "rt")) == NULL) {
  967.                     message("*** Unable to open mail file - press any key ***");
  968.                     getch();
  969.                     return NULL;
  970.                 }
  971.             }
  972.         }
  973.         while(need_edit == TRUE);
  974.  
  975.         /* valid open item here */
  976.  
  977.         rewind(fp);
  978.         in_header = TRUE;
  979.         while (fgets(buff, 511, fp) != NULL) {
  980.             if (strncmp(buff, marker, 80) == 0) {
  981.                 in_header = FALSE;
  982.                 break;
  983.             }
  984.             for (i=0;i<(int)strlen(buff);i++) {
  985.                 if (!isspace(buff[i]))
  986.                     break;
  987.                 in_header = TRUE;
  988.                 break;
  989.             }
  990.             where = ftell(fp);
  991.             if (((strnicmp(buff, "TO:", 3) == 0) ||
  992.                  (strnicmp(buff, "CC:", 3) == 0) ||
  993.                  (strnicmp(buff, "BC:", 3) == 0) ||
  994.                  (strnicmp(buff, "BCC:", 4) == 0)) && (in_header == TRUE)) {
  995.                 if (strlen(buff) < 6)
  996.                     continue;
  997.                 if ((strnicmp(buff, "BCC:", 4) == 0) &&(strlen(buff) < 7))
  998.                     continue;
  999.                 if (strnicmp(buff, "BCC:", 4) == 0)
  1000.                     strcpy(buf2, buff+5);
  1001.                 else
  1002.                     strcpy(buf2, buff+4);
  1003.                 q = strtok(buf2, " \t\r\n,");
  1004.                 while (q != '\0') {
  1005.                     if (wrk_start == NULL) {
  1006.                         wrk_start = (WRK_FILE *) malloc(sizeof(WRK_FILE));
  1007.                         wrk = wrk_start;
  1008.                         wrk->seq = seqno;
  1009.                     }
  1010.                     else {
  1011.                         wrk->next = (WRK_FILE *) malloc(sizeof(WRK_FILE));
  1012.                         wrk = wrk->next;
  1013.                         /* get another sequence number */
  1014.                         strcpy(buf3, my_stuff.spooldir);
  1015.                         strcat(buf3, "sequence.seq");
  1016.                         if (mlock(my_stuff.spooldir, "sequence", "Snews") != 0) {
  1017.                             message("*** Couldn't lock sequence file - press any key ***");
  1018.                             getch();
  1019.                             fclose(fp);
  1020.                             return NULL;
  1021.                         }
  1022.                         if ((seq_fp = fopen(buf3, "r+")) == NULL) {
  1023.                             message("*** Couldn't open mail sequence file - press any key ***");
  1024.                             getch();
  1025.                             fclose(fp);
  1026.                             return NULL;
  1027.                         }
  1028.                         /* Get the next sequence number */
  1029.                         rewind(seq_fp);
  1030.                         fscanf(seq_fp, "%ld", &seqno);
  1031.                         seqno++;
  1032.                         rewind(seq_fp);
  1033.                         fprintf(seq_fp, "%ld\n", seqno);
  1034.                         fclose(seq_fp);
  1035.                         rmlock(my_stuff.spooldir, "sequence", "Snews");
  1036.                         wrk->seq = seqno;
  1037.                     }
  1038.                     wrk->next = NULL;
  1039.                     p = strchr(q, '@');
  1040.                     if (p == NULL) {
  1041.                         sprintf(to, "%s.%s", my_stuff.my_site, my_stuff.my_domain);
  1042.                         p = to;
  1043.                     }
  1044.                     else
  1045.                         p++;
  1046.                     strcpy(wrk->next_site, p);
  1047.                     sprintf(wrk->from, "%s@%s.%s", my_stuff.user, my_stuff.my_site,
  1048.                         my_stuff.my_domain);
  1049.                     strcpy(wrk->to, q);
  1050.                     q = strtok(NULL, " \t\r\n,");
  1051.                     if (q == NULL) {
  1052.                         if (fgets(buff, 511, fp) != NULL) {
  1053.                             if (buff[0] == ' ') {
  1054.                                 strcpy(buf2, buff+1);
  1055.                                 q = strtok(buf2, " \t\n\r,");
  1056.                             }
  1057.                         }
  1058.                     }
  1059.                 }
  1060.             }
  1061.         fseek(fp, where, SEEK_SET);
  1062.         }
  1063.     }
  1064.     else
  1065.         {
  1066.         message("*** Cannot open mail file - press any key to continue ***");
  1067.         getch();
  1068.     }
  1069.  
  1070.     fclose(fp);
  1071.     return wrk_start;
  1072. }
  1073.  
  1074. /*------------------- Pass the mail to the SMTP queue ----------------------*/
  1075. void              pass_to_smtp(char *fname, WRK_FILE *wrk, int ismail)
  1076. {
  1077.     WRK_FILE         *wrk_start, *wrk_ptr;
  1078.     FILE             *fp, *txtfp, *wrkfp;
  1079.     char              buff[512], buf2[512];
  1080.     char             *p;
  1081.     int               in_header, line_ct;
  1082.     time_t            t;
  1083.     struct tm        *gmt;
  1084.  
  1085.     wrk_start = wrk;
  1086.  
  1087.     if ((fp = fopen(fname, "r")) != NULL) {
  1088.  
  1089.         /* Count the lines in the file */
  1090.         in_header = TRUE;
  1091.         while (fgets(buff, sizeof(buff), fp) != NULL) {
  1092.             if ((strncmp(buff, marker, 80) == 0) && (in_header == TRUE)) {
  1093.                 in_header = FALSE;
  1094.                 line_ct = 0;
  1095.             }
  1096.             if (in_header == FALSE)
  1097.                 line_ct++;
  1098.         }
  1099.         line_ct--;
  1100.  
  1101.         while (wrk_start != NULL) {
  1102.             if (wrk_start->seq == 0) {
  1103.         wrk_start = wrk_start->next;
  1104.                 continue;
  1105.         }
  1106.             /* create .txt file */
  1107.             sprintf(buff, "%s\\%ld.txt", my_stuff.spooldir, wrk_start->seq);
  1108.             if ((txtfp = fopen(buff, "w")) == NULL) {
  1109.                 message("*** unable to create smtp text file - press any key ***");
  1110.                 getch();
  1111.                 free_wrk(wrk);
  1112.                 return;
  1113.             }
  1114.             rewind(fp);
  1115.             in_header = TRUE;
  1116.             while (fgets(buff, sizeof(buff), fp) != NULL) {
  1117.                 if ((strncmp(buff, marker, 80) == 0) && (in_header == TRUE)) {
  1118.                     sprintf(buff, "Lines: %d\n\n", line_ct);
  1119.                     in_header = FALSE;
  1120.                 }
  1121.                 if ((strnicmp(buff, "BC:", 3) == 0) && (in_header == TRUE))
  1122.                     continue;
  1123.         if ((strnicmp(buff, "BCC:", 4) == 0) && (in_header == TRUE))
  1124.             continue;
  1125.         if ((in_header == TRUE) && (buff[0] != ' ')) {
  1126.             strcpy(buf2, buff);
  1127.             p = strtok(buf2, ":");
  1128.             p = strtok(NULL, " \t");
  1129.             if (p != NULL)
  1130.                 if (strcmp(p, "\n") == 0)
  1131.                     continue;
  1132.         }
  1133.                 fputs(buff, txtfp);
  1134.             }
  1135.             fclose(txtfp);
  1136.         rewind(fp);
  1137.             /* create .wrk file */
  1138.             sprintf(buff, "%s\\%ld.wrk", my_stuff.spooldir, wrk_start->seq);
  1139.             if ((wrkfp = fopen(buff, "w")) == NULL) {
  1140.                 message("*** unable to create smtp work file - press any key ***");
  1141.                 getch();
  1142.                 free_wrk(wrk);
  1143.                 return;
  1144.             }
  1145.             fprintf(wrkfp, "%s\n", wrk_start->next_site);
  1146.             fprintf(wrkfp, "%s\n", wrk_start->from);
  1147.             fprintf(wrkfp, "%s\n", wrk_start->to);
  1148.             wrk_start->seq = 0;                  /* mark entry as processed */
  1149.             wrk_ptr = wrk_start;
  1150.             while (wrk_ptr->next != NULL) {
  1151.                 wrk_ptr = wrk_ptr->next;
  1152.                 if (strcmp(wrk_ptr->next_site, wrk_start->next_site) == 0) {
  1153.                     fprintf(wrkfp, "%s\n", wrk_ptr->to);
  1154.                     wrk_ptr->seq = 0;
  1155.                 }
  1156.             }
  1157.             fclose(wrkfp);
  1158.             wrk_start = wrk_start->next;
  1159.         }
  1160.  
  1161.         /* add the message to the local log */
  1162.         txtfp = NULL;
  1163.         if (ismail == TRUE) {
  1164.             if (stricmp(my_stuff.maillog, "none") != 0)    {
  1165.                         sprintf(buff, "%s%s.txt", my_stuff.mail_dir, my_stuff.maillog);
  1166.                         if ((txtfp = fopen(buff, "at")) == NULL) {
  1167.                             message("*** unable to open mail log file - press any key ***");
  1168.                             getch();
  1169.                             free_wrk(wrk);
  1170.                             return;
  1171.                     }
  1172.             }
  1173.         }
  1174.         else {
  1175.             if (stricmp(my_stuff.mailuser, "none") != 0) {
  1176.                     sprintf(buff, "%s%s.txt", my_stuff.mail_dir, my_stuff.mailuser);
  1177.                         if ((txtfp = fopen(buff, "at")) == NULL) {
  1178.                              message("*** unable to open post log file - press any key ***");
  1179.                             getch();
  1180.                             free_wrk(wrk);
  1181.                                 return;
  1182.                     }
  1183.             }
  1184.         }
  1185.         if (txtfp != NULL) {
  1186.             rewind(fp);
  1187.             in_header = TRUE;
  1188.             time(&t);
  1189.             gmt = gmtime(&t);
  1190.             fprintf(txtfp, "From %s@%s.%s ", my_stuff.user, my_stuff.my_site,
  1191.                 my_stuff.my_domain);
  1192.             fprintf(txtfp, "%s, %02d %s %02d %02d:%02d:%02d GMT\n",
  1193.                 dow[gmt->tm_wday],
  1194.                 gmt->tm_mday, mth[gmt->tm_mon], (gmt->tm_year % 100),
  1195.                 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
  1196.             while (fgets(buff, sizeof(buff), fp) != NULL) {
  1197.                     if ((strncmp(buff, marker, 80) == 0) && (in_header == TRUE)) {
  1198.                         sprintf(buff, "Lines: %d\n\n", line_ct);
  1199.                                in_header = FALSE;
  1200.                 }
  1201.                     fputs(buff, txtfp);
  1202.             }
  1203.             fprintf(txtfp, "\n");
  1204.                 fclose(txtfp);
  1205.         }
  1206.         
  1207.         fclose(fp);
  1208.     if (ismail == TRUE)
  1209.         unlink(fname);
  1210.     }
  1211.     else{
  1212.         message("*** unable to open temporary mail file - press any key ***");
  1213.         getch();
  1214.     }
  1215.     free_wrk(wrk);
  1216. }
  1217.  
  1218. /*--------------- Free the memory occupied by the WRK array ----------------*/
  1219. void               free_wrk(WRK_FILE*wrk)
  1220. {
  1221.  
  1222.    WRK_FILE           *wrk_start, *wrk_ptr;
  1223.  
  1224.    wrk_start = wrk;
  1225.    while (wrk_start != NULL) {
  1226.        wrk_ptr = wrk_start;
  1227.        wrk_start = wrk_ptr->next;
  1228.        free(wrk_ptr);
  1229.        }
  1230.    wrk = NULL;
  1231. }
  1232.  
  1233.